package ks.client.gui.admin.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import ks.client.UserContext;
import ks.client.lobby.Lobby;
import ks.managers.user.User;
import ks.managers.user.UserManager;


/**
 * 
 * This controller opens the admin control window
 * whenever the admin button is clicked (by an admin).
 * 
 * @author Jennifer Hunt
 *
 */
public class AdminClickController implements ActionListener{
	
	Lobby lobby;
	UserContext context;
	
	/**
	 * Contructor for PluginAdminClickController
	 * @param lobby
	 */
	public AdminClickController(Lobby l) {
		this.lobby = l;
		context = lobby.getUserContext();
		
	}
	
	/**
	 * 
	 * When the button is clicked by an admin, the PMGUIAdmin is displayed in a new frame.
	 * 
	 */
	public void actionPerformed(ActionEvent e){
		//An action cannot be performed on this button unless the user is logged in.
		try{
		context.getUser().isEmpty();
		}catch(NullPointerException npe){
			System.out.println("This option is only available to logged in users.");
			return;
		}
		
		//Verifies that the user pressing the button is an administrator.
		UserManager um = context.getUserManager();
		User u = um.getUser(context.getUser());
		if (u == null) {}
		else if(u.isAdmin()){
			JFrame pma = lobby.getAdminControlGUI();
			pma.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			pma.setTitle("Admin Controls");
			pma.setVisible(true);
		}
		else{
			System.out.println("User is not an admin");
		}
	}

}
